﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hydrogen.MathExt.Expression
{
    /// <summary>
    /// 数学表达式执行器
    /// </summary>
    public class MathExpressionEvaluator
    {
        //private readonly ExpressionParser _parser = new ExpressionParser();
        //private readonly RPNConverter _converter = new RPNConverter();
        //private readonly ExpressionEvaluator _evaluator = new ExpressionEvaluator();

        public static double Evaluate(string expression, Dictionary<string, double> variables = null)
        {
            //List<Token> tokens = _parser.Tokenize(expression);
            //List<Token> rpn = _converter.ConvertToRPN(tokens);
            //return _evaluator.Evaluate(rpn, variables);

            List<Token> token = ExpressionParser.Tokenize(expression);
            List<Token> rpn = RPNConverter.ConvertToRPN(token);
            return ExpressionEvaluator.Evaluate(rpn, variables);
        }

        public static IExpressionEvaluator GetExpressionEvaluator(string expression)
        {
            List<Token> token = ExpressionParser.Tokenize(expression);
            List<Token> rpn = RPNConverter.ConvertToRPN(token);
            return new ExpressionEvaluator(rpn);
        }
    }
}
